<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ShopsModel extends Model {

    protected $table ="shops";

    protected $pk = 'shop_id';
    private $cacheKey="shop:id:";

    private $cacheTime=5184000;

    /*
  * 获取用户所有的商铺
  * */
    public function getShopIds($aid){
        $where = [];
        $where[] = ['is_delete',"=",0];
        if($aid == 1){
            $res = Db::name('shops')->where($where)->field("shop_id")->select();
        }else{
            $where[] = ['aid','=',$aid];
            $res = Db::name('shops')->field("shop_id")->where($where)->select();
        }
        if(empty($res)) return [];

        return array_column($res->toArray(),"shop_id");
    }

    /*
     *
     * */

    public function addOne($data){
        return Db::name('shops')->save($data);
    }


    /*
     * 删除店铺
     * */
    public function delShop($shop_id){
        return Db::name('shops')->where("shop_id","=",$shop_id)->update(["is_delete"=>1]);
    }



    /*
     * 获取total
     * */
    public function getTotal($where=[]){
        return Db::name('shops')->field("shop_id")->where($where)->count();
    }



    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('shops')->field("shop_id")->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['shop_id']);
            }
            return $return;
        }
        return [];
    }



    /*
   * 获取订单状态详情
   * */
    public function getInfo($shop_id,$isGet = true){
        $cacheKey = $this->cacheKey.$shop_id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info = json_decode($info,true);
        }else{
            $info = Db::name('shops')->find($shop_id);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }
        if($info){
            //节省性能，店铺状态进入缓存模式，不再实时取
            //$info["shop_status"] = $this->getShopStatus($shop_id);
            if($isGet){
                $productsModel = new ProductsModel();
                $shopReturnAddressesModel = new ShopReturnAddressesModel();
                $info['product_num'] = $productsModel->getShopProductNum($shop_id);
                $info['send_addresses_num'] = $shopReturnAddressesModel->getShopSendAddressesNum($shop_id);
                $info['return_addresses_num'] = $shopReturnAddressesModel->getShopReturnAddressesNum($shop_id);
            }
            return $info;
        }
        return [];
    }


    /*
     * 获取店铺实时状态
     * */
    public function getShopStatus($shop_id){
        $where[] = ["shop_id","=",$shop_id];
        return Db::name('shops')->where($where)->value("shop_status");
    }


    /*
     * 更新商铺状态
     * */
    public function updateShopStatus($shop_id, $status){
        return Db::name('shops')->where("shop_id","=",$shop_id)->update(["shop_status"=>$status]);
    }

}